package cn.yanhu.kuwanapp.my.adapter

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import cn.woochen.comlib.util.GlideUtil
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.bean.response.RespMyConfigBean
import com.youth.banner.adapter.BannerAdapter


/**
 *
 * 类描述：
 * 创建人：woochen
 * 创建时间：2020/11/2 3:07 PM
 * 修改备注：
 **/
class MyAdAdapter constructor(datas: List<RespMyConfigBean.Banner>) :
    BannerAdapter<RespMyConfigBean.Banner, MyAdAdapter.BannerViewHolder>(datas) {

    class BannerViewHolder(val view: View) : RecyclerView.ViewHolder(view){
        // 用来存放子View减少findViewById的次数
        private val mViews: SparseArray<View> = SparseArray()

        /**
         * 通过id获取view
         */
        fun <T : View?> getView(viewId: Int): T {
            // 先从缓存中找
            var view: View? = mViews.get(viewId)
            if (view == null) {
                // 直接从ItemView中找
                view = itemView.findViewById(viewId)
                mViews.put(viewId, view)
            }
            return view as T
     }
    }

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
        val itemView = LayoutInflater.from(parent?.context).inflate(R.layout.item_my_ad, parent, false)
        return BannerViewHolder(itemView)
    }

    override fun onBindView(holder: BannerViewHolder, data: RespMyConfigBean.Banner, position: Int, size: Int) {
        val ivPic = holder.getView<ImageView>(R.id.iv_pic)
        GlideUtil.loadRec(ivPic.context,data.imgUrl,ivPic,4f)
    }

}